package com.huanggf.meituan;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SearchView;

import com.huanggf.meituan.fooddetail.FoodDetailActivity;
import com.huanggf.meituan.login.LoginActivity;
import com.huanggf.meituan.utils.DateFormatUtil;

import java.io.File;
import java.util.Calendar;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private SearchView searchView;
    private SQLiteDatabase sqLiteDatabase;
    private LinearLayout img_linearlayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);  //绑定页面

        img_linearlayout = (LinearLayout) findViewById(R.id.img_linearlayout);
        searchView = (SearchView) findViewById(R.id.searchView);
        //创建数据库
        File file = new File("/data/data/com.huanggf.meituan/databases");
        if (!file.exists())
            file.mkdirs();
        sqLiteDatabase = SQLiteDatabase.openOrCreateDatabase("/data/data/com.huanggf.meituan/databases/meituan.db", null);
        //登录表
        String createTable = "CREATE TABLE IF NOT EXISTS login(id integer primary key autoincrement,username text,password text,flag integer,expire date)";
        sqLiteDatabase.execSQL(createTable);

        //登录功能
        String sel = "SELECT count(*) FROM login WHERE flag=1 AND expire>?";
        Cursor c = sqLiteDatabase.rawQuery(sel, new String[]{DateFormatUtil.dateToStr(new Date(Calendar.getInstance().getTimeInMillis()),DateFormatUtil.YMD)});

        if (c.moveToFirst()) {
            //第一次启动项目
            if (c.getInt(0) == 0) {
                //跳转主页
                Intent i = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(i);
            }
        }
        // 创建 food_detail 表
        String create_food_detail = "CREATE TABLE IF NOT EXISTS food_detail(" +
                "id integer primary key autoincrement,shop_logo text,shop_name text,grade real,sell_number integer,time varchar(10)" +
                ",distance varchar(10),start_price varchar(10),price varchar(10),avg_price varchar(10),ad1 text,ad2 text" +
                ",discount1 varchar(10),discount2 varchar(10),discount3 varchar(10),discount4 varchar(10),send_type_path text" +
                ")";
        sqLiteDatabase.execSQL(create_food_detail);

        //搜索
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                String selFoodShop = "SELECT id,shop_logo,shop_name,grade,sell_number,time,distance,start_price,price" +
                        ",avg_price,ad1,ad2,discount1,discount2,discount3,discount4,send_type_path FROM food_detail " +
                        "WHERE shop_name like '%"+query+"%' OR CAST(grade AS TEXT) like '%"+query+"%' OR CAST(sell_number AS TEXT) like '%"+query+"%' " +
                        "OR time like '%"+query+"%' OR distance like '%"+query+"%' " +
                        "OR start_price like '%"+query+"%' " +
                        "OR price like '%"+query+"%' OR avg_price like '%"+query+"%' OR ad1 like '%"+query+"%' OR ad2 like '%"+query+"%' " +
                        "OR discount1 like '%"+query+"%' OR discount2 like '%"+query+"%' OR discount3 like '%"+query+"%' " +
                        "OR discount4 like '%"+query+"%'";
                Intent intent = new Intent(MainActivity.this, FoodDetailActivity.class);
                Bundle datas = new Bundle();
                datas.putString("querySQL", selFoodShop);
                datas.putString("queryData", query);
                intent.putExtras(datas);
                startActivityForResult(intent, 1);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String query) {
                return false;
            }
        });

        //点击查看美食详情页面
        img_linearlayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //页面跳转

                Intent intent = new Intent(MainActivity.this, FoodDetailActivity.class);
                startActivityForResult(intent, 1);
            }
        });



        //商品表
        String merchandiseTable = "CREATE TABLE IF NOT EXISTS merchandise(id integer primary key autoincrement,m_name text,m_img_path text,price real,food_id integer)";
        sqLiteDatabase.execSQL(merchandiseTable);
        //订单-商品表
        String order_merchandiseTable = "CREATE TABLE IF NOT EXISTS order_merchandise(id integer primary key autoincrement,merchandise_id integer,m_order_id integer)";
        sqLiteDatabase.execSQL(order_merchandiseTable);
        //订单表
        String orderTable = "CREATE TABLE IF NOT EXISTS m_order(id integer primary key autoincrement,food_id integer,create_time datetime,flag varchar(1),reserve varchar(1),reserve_time time)";
        sqLiteDatabase.execSQL(orderTable);


    }

    @Override
    protected void onDestroy() {
        //关闭数据库
        super.onDestroy();
        if (sqLiteDatabase != null && sqLiteDatabase.isOpen()) {
            sqLiteDatabase.close();
        }
    }
}
